Skip to content

Add my-calendar#2

Open
hiro23900 wants to merge 5 commits into
mainfrom
my-calendar
Open

Add my-calendar#2
hiro23900 wants to merge 5 commits into
mainfrom
my-calendar

Conversation

@hiro23900

Copy link
Copy Markdown
Owner

No description provided.

Comment thread 02.calendar/cal.rb Outdated
default_year = Date.today.year.to_s
default_month = Date.today.month.to_s

options = ARGV.getopts("y:m:", "y:#{default_year}", "m:#{default_month}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARGV.getopts の第2引数以降はロングオプションの定義に使うもので、デフォルト値を指定する方法ではありません。

options = ARGV.getopts("y:m:", "y:#{default_year}", "m:#{default_month}")

実際には "y:2026" のような文字列をロングオプション名として解釈しようとするため、意図した動作になっていない可能性があります。

デフォルト値は getopts の戻り値に対して別途設定するのが一般的です。例えば:

options = ARGV.getopts("y:m:")
year  = (options["y"] || Date.today.year.to_s).to_i
month = (options["m"] || Date.today.month.to_s).to_i

現在の提出物のエビデンスでは引数なしでも動いているように見えますが、動作原理を確認してみてください。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指摘ありがとうございます。
デフォルト設定は、確かにRubyリファレンスにはロングオプションに対する記述となっておりました。ただ、私が勝手にショートオプションに対するデフォルト値の設定が用意されていると決め付けてしまい、ちょうどロングオプションのやりかたで思わく通り動作しましたので、これで良しとしてしまいました。
今後はRubyリファレンスにもっと意識を向けたいと思います。
また、対処方法のアドバイス有難うございます。使用させていただきます。変数に初期設定値がなければ指定できますので、多くのケースで利用できると思います。ご提案ありがとうございました。

Comment thread 02.calendar/cal.rb

# 日を出力
(first_day..last_day).each do |d|
if d.day == 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1日目のインデント計算で 2 + first_day.wday * 3 としていますが、これは少し崩れる場合があります。

他の日は rjust(3) で幅3を使っています(日曜日だけ rjust(2))。1日目も同じ幅3を基準に考えると、インデントは first_day.wday * 3 ですが、日曜始まりのとき(wday == 0)は rjust(2) にする必要があります。

calコマンドと実際に出力を比較して、特に日曜始まりの月(例:2026年2月など)で幅がズレていないか確認してみてください。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1日目のインデント計算を「2 + first_day.wday * 3」としております。
自分の考えは以下の通りです。

first_dayは、Dateオブジェクトです。
「2 + first_day.wday * 3」の部分の計算は以下となります。
1日目が日曜の場合、rjust(2 + 0 * 3) = rjust(2) <= 日曜だけ幅2
1日目が月曜の場合、rjust(2 + 1 * 3) = rjust(5) <= 幅3
1日目が火曜の場合、rjust(2 + 2 * 3) = rjust(8) <= 幅3
1日目が水曜の場合、rjust(2 + 3 * 3) = rjust(11) <= 幅3
1日目が木曜の場合、rjust(2 + 4 * 3) = rjust(14) <= 幅3
1日目が金曜の場合、rjust(2 + 5 * 3) = rjust(17) <= 幅3
1日目が土曜の場合、rjust(2 + 6 * 3) = rjust(20) <= 幅3

故に日曜だけ幅2となることを考慮しております。
この部分については現状としたいと思います。

Comment thread 02.calendar/cal.rb
last_day = Date.new(year, month, -1)

# タイトルを出力
puts (month.to_s + "月" + " " + year.to_s).center(20)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ヘッダー行の組み立てを確認してください。

puts (month.to_s + "月" + " " + year.to_s).center(20)

center(20) の幅 20 ですが、calコマンドの出力では曜日行「日 月 火 水 木 金 土」が20文字(半角換算)あり、ヘッダーはその幅に合わせて中央揃えされます。日本語文字は全角なので、center に渡す幅の計算に注意が必要です。

提出物エビデンスの出力を見ると 11月 2026 となっており、calコマンドの 11月 2026 と大体合っているように見えますが、Rubyの center はバイト数や文字数の扱いが全角/半角混在時に想定と異なる場合があります。さまざまな月(1桁月・2桁月)で calコマンドと並べて確認してみてください。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提出エビデンスはターミナルの出力結果のテキストをコピペしただけでしたので、提出エビデンスの貼り付け先のWebの使用フォントにより微妙にヘッダー部分と日のデータ部分がずれているように見えておりました。先ほど、ターミナルの出力結果の画像キャプチャを貼り付けました。ヘッダー部分と日のデータ部分はずれておりません。また、calコマンドと同じ出力結果となっております。

Comment thread 02.calendar/cal.rb Outdated
require "optparse"

default_year = Date.today.year.to_s
default_month = Date.today.month.to_s

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6〜7行目の default_yeardefault_month ですが、この後のコードでどこにも使われていません。|| でデフォルト値を指定する方式に修正したときに不要になった変数が残ったままになっています。削除してすっきりさせましょう。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要になった変数の削除の意識が不足しておりました。削除いたします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants